package com.yswy.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.*;
import com.yswy.common.enums.TfPlatformEnums;
import com.yswy.common.enums.XtPlatformEnums;
import com.yswy.common.enums.base.TypeEnum;
import com.yswy.common.utils.StringUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.entity.PackageVersion;
import com.yswy.domain.entity.tf.TgChannel;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.tf.TgChannelMapper;
import com.yswy.domain.vo.gld.addOrUpate.TgChannelAddOrUpdate;
import com.yswy.domain.vo.gld.addOrUpate.base.BaseReqVO;
import com.yswy.domain.vo.gld.list.TgChannelList;
import com.yswy.domain.vo.gld.search.TgChannelSearch;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.validation.constraints.NotNull;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;


@Slf4j
@Service
public class TgChannelServiceImpl extends ServiceImpl<TgChannelMapper, TgChannel>
        implements TgChannelService {
    @Autowired
    private SsoService ssoService;

    @Autowired
    private PackageManageService packageManageService;

    @Autowired
    private TgEventService tgEventService;

    @Autowired
    private ISysConfigService sysConfigService;
    @Override
    public Page<TgChannelList> pageTgChannel(TgChannelSearch res) {
        if (StringUtils.isNotEmpty(res.getXtPlatform())){
            res.setXtPlatform(TypeEnum.messageOf(XtPlatformEnums.class , res.getXtPlatform()).toString());
        }
        if (StringUtils.isNotEmpty(res.getTfPlatform())){
            res.setTfPlatform(TypeEnum.messageOf(TfPlatformEnums.class , res.getTfPlatform()).toString());
        }

        Page<TgChannelList> tgChannelListPage = baseMapper.pageTgChannelList(res
                ,ssoService.isAdminToUserId(CurrentUserContext.getCurrentUser())
                , new Page<>(res.getPageNum() , res.getPageSize()) );
        for (TgChannelList tgChannelList:tgChannelListPage.getRecords()){
            TypeEnum xtPlatformEnum = TypeEnum.typeEnumOf(XtPlatformEnums.class , tgChannelList.getXtPlatform());
            tgChannelList.setXtPlatform(xtPlatformEnum.getMessage());
            TypeEnum tfPlatformEnum = TypeEnum.typeEnumOf(TfPlatformEnums.class , tgChannelList.getTfPlatform());
            tgChannelList.setTfPlatform(tfPlatformEnum.getMessage());
        }

        return tgChannelListPage;
    }

    @Override
    public void addTgChannel(TgChannelAddOrUpdate res) {
        Boolean isExistchannel =  isExistTgChannel(res.getChannel());
        if (isExistchannel){
            throw new IllegalStateException("检测链接已存在");
        }
        TgChannel tgChannel = new TgChannel();
        PackageManage packageManage = packageManageService.getById(res.getPackageManageId());
        tgChannel.setPackageManageId(res.getPackageManageId());
        tgChannel.setAppProjectId(packageManage.getProjectId());
        tgChannel.setTgDeductionId(res.getTgDeductionId());
        tgChannel.setTgEventId(res.getTgEventId());
        tgChannel.setChannel(res.getChannel());
        tgChannel.setRemarks(res.getRemarks());
        if (res.getExaminePrice() != null){
            tgChannel.setExaminePrice(res.getExaminePrice());
        }else {
            tgChannel.setExaminePrice(0L);
        }
        tgChannel.setIsOpen(res.getIsOpen());
        //生产检测链接
        String urlMsg = "";
        TypeEnum tfPlatformEnums = TypeEnum.typeEnumOf(TfPlatformEnums.class , packageManage.getTfPlatform());

        String zjym= sysConfigService.selectConfigByKey("dominUrl");

        if (tfPlatformEnums.getMessage().equals(TfPlatformEnums.JLYQ.getMessage())){
            urlMsg = zjym+"call/jlCallBack?&type=0&channel="+ res.getChannel()+
                    "packChannel="+packageManage.getChannel()+
                    "projectId="+packageManage.getProjectId()+
                    "&DID=__DID__" +
                    "&AID=__AID__" +
                    "&CID=__CID__" +
                    "&DNAME=__DNAME__" +
                    "&UNITNAME=__UNITNAME__" +
                    "&OAID2=__OAID2__" +
                    "&IMEI2=__IMEI2__" +
                    "&IDFA2=__IDFA2__" +
                    "&MAC2=__MAC2__" +
                    "&ANDROIDID2=__ANDROIDID2__" +
                    "&UA=__UA__"+
                    "&OS=__OS__" +
                    "&MODEL=__MODEL__" +
                    "&CALLBACK=__CALLBACK__" +
                    "&IPV4=__IPV4__" ;
        }else if (tfPlatformEnums.getMessage().equals(TfPlatformEnums.CLZT.getMessage())){
            urlMsg = zjym+"call/ksCallback?&type=0&channel="+ res.getChannel()+
                    "packChannel="+packageManage.getChannel()+
                    "projectId="+packageManage.getProjectId()+
                    "&ADVERTISER_ID=__ADVERTISER_ID__" +
                    "&CSITE=__CSITE__" +
                    "&CONVERT_ID=__CONVERT_ID__" +
                    "&IMEI=__IMEI__" +
                    "&ANDROIDID=__ANDROIDID__" +
                    "&OAID=__OAID__" +
                    "&CALLBACK_PARAM=__CALLBACK_PARAM__" +
                    "&OS=__OS__" +
                    "&MAC=__MAC__" +
                    "&IP=__IP__" +
                    "&MODEL=__MODEL__";
        }
        tgChannel.setUrlMsg(urlMsg);
        save(tgChannel);



    }

    @Override
    public void updateTgChannel(TgChannelAddOrUpdate req) {
        //更新数据
        TgChannel bo = getById(req.getId());
        Assert.state(null != bo && null != bo.getId(), NULL_MSG);
        bo.setRemarks(req.getRemarks());
        bo.setExaminePrice(req.getExaminePrice());
        bo.setIsOpen(req.getIsOpen());
        bo.setTgDeductionId(req.getTgDeductionId());
        bo.setTgEventId(req.getTgEventId());
        updateById(bo);
        //清理扣量配置缓存
        tgEventService.clearTgEventCacheByChannel(bo.getChannel());
    }

    @Override
    public Boolean isExistTgChannel(String channel) {
        Integer count = count(new LambdaQueryWrapper<TgChannel>()
                .eq(TgChannel::getChannel , channel)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }

        return false;
    }

    @Override
    public void delTgChannel(Long tgChannelId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, tgChannelId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        TgChannel bo = new TgChannel();
        bo.setId(tgChannelId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }
}
